package com.init.iblog.Controller;

import com.init.iblog.domain.ResponseResult;
import com.init.iblog.domain.dto.CategoryDto;
import com.init.iblog.domain.entity.Category;
import com.init.iblog.domain.entity.User;
import com.init.iblog.domain.vo.CategoryVo;
import com.init.iblog.domain.vo.PageVo;
import com.init.iblog.service.CategoryService;
import com.init.iblog.utils.SecurityUtils;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/content/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 查询所有目录
     * @return
     */
    @GetMapping("/listAllCategory")
    public ResponseResult listAllCategory(){
        System.out.println("=============进入查询所有目录接口/content/category/listAllCategory============");
        List<CategoryVo> list = categoryService.listAllCategory();
        return ResponseResult.okResult(list);
    }


    /**
     * 分页查询分类列表
     * @param pageNum
     * @param pageSize
     * @param categoryDto
     * @return
     */
    @GetMapping("/list")
    public ResponseResult<PageVo> listCategory(Integer pageNum, Integer pageSize, CategoryDto categoryDto){
        System.out.println("============进入分页查询分类接口===========");
        return categoryService.pageCategoryList(pageNum,pageSize,categoryDto);
    }


    /**
     * 查询分类详情
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResponseResult getCategory(@PathVariable Long id){
        System.out.println("=========查询分类详情=========");
        Category category = categoryService.getById(id);
        return ResponseResult.okResult(category);
    }


    /**
     * 新增分类
     * @param category
     * @return
     */
    @PostMapping
    public ResponseResult addCategory(@RequestBody Category category){

        System.out.println("=========进入新增分类接口============");
        User user = SecurityUtils.getLoginUser().getUser();
        Long create_by = user.getId();
        category.setCreateBy(create_by);
        category.setCreateTime(new Date());

        return categoryService.addCategory(category);
    }


    /**
     * 修改分类
     * @param category
     * @return
     */
    @PutMapping
    public ResponseResult updateCategory(@RequestBody Category category){
        System.out.println("=======修改分类接口=========");
        return categoryService.updateCategory(category);
    }


    /**
     * 删除分类
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public ResponseResult delCategory(@PathVariable List<Long> id){
        System.out.println("=======进入删除分类接口========");

        return categoryService.deleteCategory(id);
    }

    @GetMapping
    public ResponseResult exportCategory(){
        return null;
    }

}

